package com.ms.p2p.plugin.payment;


import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.Constants;
import com.ms.p2p.entity.account.AccountRecharge;
import com.ms.p2p.entity.system.PayMethod;

import com.ms.p2p.plugin.AutoRegisterPlugin;
import com.ms.p2p.service.MemberAccountRechargeService;
import com.ms.p2p.service.PayMethodService;
import com.ms.p2p.service.PayMethodValueService;
import com.ms.p2p.service.SettingService;
import com.ms.p2p.utils.TradeUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * 支付插件基类
 */
public abstract class AbstractPaymentPlugin extends AutoRegisterPlugin {

	@Autowired
	public PayMethodService payMethodService;
	@Autowired
	public PayMethodValueService payMethodValueService;
	@Autowired
	public MemberAccountRechargeService rechargeService;
	@Autowired
	public SettingService settingService;

	public AccountRecharge BuildReCharge(Integer userId, BigDecimal money, PayMethod payment) throws IllegalAccessException, IntrospectionException, InvocationTargetException {
		AccountRecharge recharge = new AccountRecharge();
		recharge.setUser_id(userId);
		recharge.setAddtime(MsDateUtils.getTime());
		recharge.setAddip(AppContext.getRemoteIp());
		recharge.setStatus(0);
		recharge.setMoney(money);
		recharge.setFee(BigDecimal.ZERO);
		recharge.setAmount(money);
		recharge.setType(1);
		recharge.setPayment(Integer.parseInt(getId()));
		recharge.setTrade_no(TradeUtil.getTrano()+"");
		recharge.setRemark(payment.getName() + "-" + payment.getDescription());
		rechargeService.insert(recharge);
		return recharge;
	}

	/**
	 * 建立请求，以表单HTML形式构造（默认）
	 * @param sParaTemp 请求参数数组
	 * @param strMethod 提交方式。两个值可选：post、get
	 * @param strButtonName 确认按钮显示文字
	 * @return 提交表单HTML文本
	 */
	public static String buildRequest(LinkedHashMap map, String action) {
		//待请求参数数组
		Iterator iter = map.entrySet().iterator();
		String str = "" ;
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			str=str+"&"+entry.getKey() + "=" + entry.getValue();
		}
		str=str.substring(1);
		return "redirect:" + action+str;

	}
	public static String buildPostRequest(LinkedHashMap map, String action, HttpServletRequest request) {
		request.setAttribute("maps",map);
		request.setAttribute("url",action);
		return "/common/form/submit";

	}



	public  String OnlineReturn(String requestId, String name) throws IllegalAccessException, IntrospectionException, InvocationTargetException {
		AccountRecharge recharge = rechargeService.findByTrano(requestId,0);
		if(recharge==null){
			return Constants.HASPAY;
		}

		rechargeService.onlineRecharge(AppContext.getRemoteIp(),recharge, name);
		return  Constants.SUCCESS;

	}
	protected boolean isRecharge(int status){
		return (status==1)?true:false;
	}

	public String getRequstType() {
		return "form";
	}

	public String getEncode(){
		return "utf8";
	}
}
